package pc.system;

import java.sql.*;

import pc.util.*;

public class Record
{
  private ResultSet rs = null;
  private boolean empty = true;

  public Record()
  {
    //empty = true;
  }

  public Record(ResultSet rs)
  {
    try {
      if (rs != null) empty = !rs.first();
      this.rs =  (!empty) ? rs : null;
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void setRs(ResultSet rs)
  {
    this.rs = rs;
    empty = (rs == null) ;
  }

  public ResultSet getRs()
  {
    return this.rs;
  }

  public int findColumn(String colName)
  {
    if (rs == null) return -1;
    try {
      return rs.findColumn(colName);
    } catch (SQLException e) {
      return -1;
    }
  }

  public boolean absolute(int row)
  {
    if (rs == null) return false;
    try {
      return rs.absolute(row);
    } catch (SQLException e){
      e.printStackTrace();
      return false;
    }
  }

  public boolean first()
  {
    if (rs == null) {
      empty = true;
      return false;
    }
    try {
      empty = !rs.first();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return !empty;
  }

  public boolean last()
  {
    if (rs == null) return false;
    try {
      return rs.last();
    } catch (SQLException e){
      e.printStackTrace();
      return false;
    }
  }

  public boolean next()
  {
    if (this.rs == null) return false;
    try {
      return this.rs.next();
    } catch (SQLException e) {
      e.printStackTrace();
      return false;
    }
  }

  public boolean isAfterLast()
  {
    if (rs == null) return true;
    try {
      return rs.isAfterLast();
    }  catch (SQLException e) {
      e.printStackTrace();
      return true;
    }
  }

  public void beforeFirst()
  {
    if (this.rs == null) return;
    try {
      this.rs.beforeFirst();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public int getRow()
  {
    try {
      return this.rs.getRow();
    } catch (SQLException e) {
      e.printStackTrace();
      return 0;
    }
  }

  public boolean isEmpty()
  {
    return (rs == null || empty);
  }

  public int getCount()
  {
    int row = getRow();
    if (!last()) return 0;
    int count = getRow();
    absolute(row);
    return count;
  }

  public void close()
  {
    try {
      if (rs != null) rs.close();
      this.setRs(null);
    }  catch (SQLException ex) {
      ex.printStackTrace();
    }
  }



  public String getString(String field)
  {
    try {
      if (rs != null && !empty) {
        String s = rs.getString(field);
        return s != null ? s : "";
      }
    }  catch (SQLException e) {
      e.printStackTrace();
    }
    return "";
  }
  
  public String getDate(String field)
  {
    try {
      if (rs != null  && !empty) return Utils.dateToStr(rs.getDate(field));
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return "";
  }

  public String getTime(String field)
  {
    try {if (rs != null && !empty) return Utils.timeToStr(rs.getTime(field));}
    catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getInt(String field)
  {
    try {if (rs != null && !empty) return String.valueOf(rs.getInt(field));}
    catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getLong(String field)
  {
    try {
      if (rs != null && !empty) return String.valueOf(rs.getLong(field));
    } catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getDouble(String field, int dec)
  {
    try {
      if (rs != null && !empty) return Utils.numberToStr(rs.getDouble(field),dec);
    } catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getDouble(String field)
  {
    return getDouble(field,4);
  }

  public String getFloat(String field, int dec)
  {
    try {
      if (rs != null && !empty)
        return Utils.numberToStr(rs.getFloat(field),dec);
    } catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getFloat(String field)
  {
    return getFloat(field,4);
  }

  public String getBigDecimal(String field)
  {
    try {
      if (rs != null && !empty) return Utils.numberToStr(rs.getBigDecimal(field));
    } catch (SQLException e) { e.printStackTrace();}
    return "";
  }

  public String getStringValue(String field)
  {
    try {if (rs != null && !empty) return rs.getString(field);}
    catch (SQLException e) { e.printStackTrace();}
    return null;
  }

  public char getCharValue(String field)
  {
    String s = getStringValue(field);
    if (s == null || s.length() == 0) s = " ";
    return s.charAt(0);
  }

  public long getLongValue(String field)
  {
    try {if (rs != null && !empty) return rs.getLong(field);}
    catch (SQLException e) { e.printStackTrace();}
    return 0;
  }

  public int getIntValue(String field)
  {
    try {if (rs != null && !empty) return rs.getInt(field);}
    catch (SQLException e) { e.printStackTrace();}
    return 0;
  }

  public float getFloatValue(String field)
  {
    try {if (rs != null && !empty) return rs.getFloat(field);}
    catch (SQLException e) { e.printStackTrace();}
    return 0;
  }

  public double getDoubleValue(String field)
  {
    try {if (rs != null && !empty) return rs.getDouble(field);}
    catch (SQLException e) { e.printStackTrace();}
    return 0;
  }

  public java.sql.Date getDateValue(String field)
  {
    try {if (rs != null && !empty) return rs.getDate(field);}
    catch (SQLException e) {e.printStackTrace();}
    return null;
  }

  public java.sql.Time getTimeValue(String field)
  {
    try {if (rs != null && !empty) return rs.getTime(field);}
    catch (SQLException e) {e.printStackTrace();}
    return null;
  }

  public String formatPercent(String field)
  {
    float num = 0;
    try {if (rs != null && !empty) num = rs.getFloat(field);}
    catch (SQLException e) { e.printStackTrace();}
    return Utils.numberToStr(num + 0.0001,2);
  }

  public String getPercent(String field,String pad)
  {
    float num = 0;
    try {if (rs != null && !empty) num = rs.getFloat(field);}
    catch (SQLException e) { e.printStackTrace();}
    if (num == 0) return "-" + pad;
    else return Utils.numberToStr(num + 0.0001,2);
  }

  public String getPercent(String field)
  {
    return getPercent(field,"");
  }
}
